package com.cognizant.ams.controller.ldcode;

import com.alibaba.fastjson.JSONArray;
import com.cognizant.ams.bean.Ldcode;
import com.cognizant.ams.bean.LdcodeExample;
import com.cognizant.ams.common.redis.RedisLockUtil;
import com.cognizant.ams.common.redis.RedisUtil;
import com.cognizant.ams.common.util.CognizantResult;
import com.cognizant.ams.dao.LdcodeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @author suyon
 * @date 2019-11-13 10:05
 */
@RestController
@RequestMapping("html/sys/ldcode")
@Api(tags = "数据字典接口")
//@Slf4j
public class LdCodeController {

    @Autowired
    private LdcodeMapper ldcodeMapper;

    /**
     * RedisLockUtil.
     */
    @Autowired
    private RedisLockUtil redisLockUtil;

    /**
     * LOCK_KEY.
     */
    private static final String LOCK_KEY = "LOCK_CONTRACT";


    @GetMapping("/tryLock")
    public CognizantResult isLock(String name) throws InterruptedException {
        //假设 这是一次巨量的修改或新增操作
        //log.info("当前操作人:{}", name);
        boolean tryLock = redisLockUtil.tryLock(LOCK_KEY);
        if (tryLock) {
            RedisUtil.set("THIS_SERVER:" + LOCK_KEY, name);
            StopWatch watch = new StopWatch();

            watch.start();
            Thread.sleep(5000);
            watch.stop();
            //log.info("【本次任务结束，消耗时间】：{}ms", watch.getTotalTimeMillis());

            redisLockUtil.releaseLock(LOCK_KEY);
            RedisUtil.del("THIS_SERVER:" + LOCK_KEY);
        } else {
            String thisName = RedisUtil.get("THIS_SERVER:" + LOCK_KEY);
            //log.info("{}正在尝试该操作，请勿重复提交", thisName);
            return CognizantResult.error(thisName + "正在尝试该操作，请勿重复提交");
        }

        return CognizantResult.ok();
    }

    @GetMapping("/{model}/all")
    @ApiOperation("通过Code获取对应字典")
    @ApiImplicitParam(name = "model", value = "codeType", required = true, dataType = "String", paramType = "path")
    public CognizantResult getLdCodesByModel(@PathVariable String model) {

        String codes = RedisUtil.get(model);
        List resultList = JSONArray.parseArray(codes, Ldcode.class);

        if (CollectionUtils.isNotEmpty(resultList)) {
            return CognizantResult.ok().put("ldCodes", resultList);
        }

        //1.根据字典类型获取相关分组
        LdcodeExample ldcodeExample = new LdcodeExample();
        LdcodeExample.Criteria param = ldcodeExample.createCriteria();
        param.andCodetypeEqualTo(model);

        List<Ldcode> ldCodeList = ldcodeMapper.selectByExample(ldcodeExample);

        //2.压入Redis
        String jsonArray = JSONArray.toJSON(ldCodeList).toString();
        RedisUtil.setEx(model, jsonArray, -1);

        return CognizantResult.ok().put("ldCodes", jsonArray);
    }

}
